//
//  DownloadedViewController.m
//  MXClassroom
//
//  Created by 你个LB on 16/9/11.
//  Copyright © 2016年 你个LB. All rights reserved.
//

#import "DownloadedViewController.h"

@interface DownloadedViewController ()

@end

@implementation DownloadedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initDataSource];
    
    [self initTableView];
    
    // 排序类别数组
    chooseArray = [NSMutableArray arrayWithArray:@[@[@"手动排序",@"按时间排序",@"按大小排序"]]];
    
    // 排序控件
    DropDownListView * dropDownView = [[DropDownListView alloc] initWithFrame:CGRectMake(0,0, kScreen_width / 2, 40) dataSource:self delegate:self];
    [dropDownView hideExtendedChooseView];
    dropDownView.mSuperView = self.view;
    [self.view addSubview:dropDownView];
    
    // 分割线
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(kScreen_width / 2, 5, 0.5, 30)];
    lineView.backgroundColor = kColor_Memory_text_color;
    [self.view addSubview:lineView];
    
    // 一键清除按钮
    UIButton * cleanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cleanBtn.frame = CGRectMake(kScreen_width / 2, 0, kScreen_width / 2, 40);
    [cleanBtn setImage:[UIImage imageNamed:@"下载中_一键清空38px.png"] forState:UIControlStateNormal];
    [cleanBtn setTitle:@"一键清除" forState:UIControlStateNormal];
    [cleanBtn setTitleColor:kColor_Memory_text_color forState:UIControlStateNormal];
    cleanBtn.titleLabel.font = [UIFont systemFontOfSize:13 weight:2];
    [cleanBtn addTarget:self action:@selector(cleanBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cleanBtn];
}

- (void)cleanBtnAction{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"清除下载" message:@"清除后将不可恢复" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"buttonIndex:%ld",buttonIndex);
}

#pragma mark -- dropDownListDelegate
-(void) chooseAtSection:(NSInteger)section index:(NSInteger)index
{
    NSLog(@"选了section:%ld ,index:%ld",section,index);
}

#pragma mark -- dropdownList DataSource
-(NSInteger)numberOfSections
{
    return [chooseArray count];
}
-(NSInteger)numberOfRowsInSection:(NSInteger)section
{
    NSArray *arry =chooseArray[section];
    return [arry count];
}
-(NSString *)titleInSection:(NSInteger)section index:(NSInteger) index
{
    return chooseArray[section][index];
}
-(NSInteger)defaultShowSection:(NSInteger)section
{
    return 0;
}

// 单元格数据
- (void)initDataSource{
    
    NSMutableArray * models = [NSMutableArray array];
    NSArray * data = @[@{@"headImg":@"icon_main_recommend_science_normal.png",
                         @"title":@"标题",
                         @"teacher":@"我佛慈悲",
                         @"teachTime":@"1天前",
                         @"fileSize":@"3.4M",
                         @"downloadState":@"Downloaded",
                         @"downloadPrograss":@"1"},
                       @{@"headImg":@"icon_main_recommend_science_normal.png",
                         @"title":@"标题2",
                         @"teacher":@"我佛慈悲2",
                         @"teachTime":@"2天前",
                         @"fileSize":@"3.4M",
                         @"downloadState":@"Downloaded",
                         @"downloadPrograss":@"1"}];
    
    for (int i = 0; i < 2; i ++) {
        DownloadModel * download = [[DownloadModel alloc] initWithContentDic:data[i]];
        [models addObject:download];
    }
    _dataList = models;
}

- (void)initTableView{
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, kScreen_width, kScreen_height - 64 - 40) style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DownloadTableViewCell * cell = [DownloadTableViewCell cellWithTableView:tableView];
    cell.download = _dataList[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 12;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 86;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
